package backtrack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author : Rookie
 * @description : 电话号码字母组合
 * @date : 2023/11/12
 */
public class PhoneNumLetterAssembly {

    private static final HashMap<Integer,String> letterMap = new HashMap<>();

    private static final StringBuilder temp = new StringBuilder();

    static {
        letterMap.put(0,"");
        letterMap.put(1,"");
        letterMap.put(2,"abc");
        letterMap.put(3,"def");
        letterMap.put(4,"ghi");
        letterMap.put(5,"jkl");
        letterMap.put(6,"mno");
        letterMap.put(7,"pqrs");
        letterMap.put(8,"tuv");
        letterMap.put(9,"wxyz");
    }

    public static void main(String[] args) {
        int[] input = new int[] {2,3};
        List<String> vals = new ArrayList<>();
        for (int e: input) {
            String val = letterMap.get(e);
            vals.add(val);
        }
        ArrayList<String> result = new ArrayList<>();
        backtrack(vals,0,result);
        System.out.println(result);
    }

    private static void backtrack(List<String> inputs, int index,List<String> result) {
        // inputs遍历完成
        if (index == inputs.size()) {
            result.add(temp.toString());
            return;
        }
        String letter = inputs.get(index);
        for (int i = 0; i < letter.length(); i++) {
            temp.append(letter.charAt(i));
            // 选择下一个数据对应的字符
            backtrack(inputs,index+1,result);
            // 回溯
            temp.deleteCharAt(temp.length()-1);
        }
    }
}
